﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Library
{
    public partial class ReaderBookForm : Form
    {
        LibraryEntities1 ins = new LibraryEntities1();
        public static ObjectSet <Reader> readers;
        public static ObjectSet<Student> students;
        public static ObjectSet<Employee> employers;
        public static ObjectSet<Professor> professors;
        public ReaderBookForm()
        {
            InitializeComponent();
            readers = ins.Reader;
            students = ins.Student;
            employers = ins.Employee;
            professors = ins.Professor;
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReaderViewForm fm = new ReaderViewForm(null, true);
            fm.ShowDialog();
            if (fm.reader != null)
            {
                bookView.Rows.Add();

                bookView.Rows[bookView.Rows.Count - 1].Cells[0].Value = fm.reader.ReaderId;
                bookView.Rows[bookView.Rows.Count - 1].Cells[1].Value = fm.reader.FIO;
                bookView.Rows[bookView.Rows.Count - 1].Cells[2].Value = fm.reader.Passport;
                bookView.Rows[bookView.Rows.Count - 1].Cells[3].Value = fm.reader.ReadersCard;
                bookView.Rows[bookView.Rows.Count - 1].Cells[4].Value = fm.reader.Address;
                bookView.Rows[bookView.Rows.Count - 1].Cells[5].Value = fm.reader.TelHome;
                bookView.Rows[bookView.Rows.Count - 1].Cells[6].Value = fm.reader.TelWork;
            }
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ReaderBookForm_Load(object sender, EventArgs e)
        {
            bookView.Columns.Add("IdReader", "IdReader");
            bookView.Columns["IdReader"].Visible = false;

            bookView.Columns.Add("FIO", "Читатель");
            bookView.Columns["FIO"].Width = 200;

            bookView.Columns.Add("Passport", "Паспорт");
            bookView.Columns["Passport"].Width = 80;

            bookView.Columns.Add("ReaderCard", "Читательский");
            bookView.Columns["ReaderCard"].Width = 100;

            bookView.Columns.Add("Address", "Адрес");
            bookView.Columns["Address"].Width = 200;

            bookView.Columns.Add("HomeTel", "Дом. тел.");
            bookView.Columns["HomeTel"].Width = 100;

            bookView.Columns.Add("WorkTel", "Раб. тел.");
            bookView.Columns["WorkTel"].Width = 100;

            int i = 0;
            foreach (Reader x in readers)
            {
                bookView.Rows.Add();
                bookView.Rows[i].Cells[0].Value = x.ReaderId;
                bookView.Rows[i].Cells[1].Value = x.FIO;
                bookView.Rows[i].Cells[2].Value = x.Passport;
                bookView.Rows[i].Cells[3].Value = x.ReadersCard;
                bookView.Rows[i].Cells[4].Value = x.Address;
                bookView.Rows[i].Cells[5].Value = x.TelHome;
                bookView.Rows[i].Cells[6].Value = x.TelWork;
                i++;
            }
        }

        private void редактироватьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ins = new LibraryEntities1();
            readers = ins.Reader;

            if (bookView.CurrentRow != null)
            {

                int id = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                Reader reader = readers.Where(x => x.ReaderId == id).FirstOrDefault();
                ReaderViewForm fm = new ReaderViewForm(reader, false);
                fm.ShowDialog();
                bookView.CurrentRow.Cells[1].Value = fm.reader.FIO;
                bookView.CurrentRow.Cells[2].Value = fm.reader.Passport;
                bookView.CurrentRow.Cells[3].Value = fm.reader.ReadersCard;
                bookView.CurrentRow.Cells[4].Value = fm.reader.Address;
                bookView.CurrentRow.Cells[5].Value = fm.reader.TelHome;
                bookView.CurrentRow.Cells[6].Value = fm.reader.TelWork;
            }
            else
            {
                MessageBox.Show("Выберите запись для редактирования");
            }
        }

        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {

                int elementId = Convert.ToInt32(bookView.CurrentRow.Cells[0].Value);
                if (readers.Where(x => x.ReaderId == elementId).Count() == 1)
                {
                    Reader element = readers.Where(x => x.ReaderId == elementId).FirstOrDefault();
                    if (element.InstanceBook.Count > 0)
                        MessageBox.Show("Невозможно удаление. У пользователя на руках книги");
                    else
                    {
                        Student student = students.Where(x => x.ReaderId.Equals(element.ReaderId)).FirstOrDefault();
                        if (student != null)
                        {
                            students.DeleteObject(student);
                            ins.SaveChanges();
                        }
                        Employee employee = employers.Where(x => x.ReaderId.Equals(element.ReaderId)).FirstOrDefault();
                        if (employee != null)
                        {
                            employers.DeleteObject(employee);
                            ins.SaveChanges();
                        }
                        Professor professor = professors.Where(x => x.ReaderId.Equals(element.ReaderId)).FirstOrDefault();
                        if (professor != null)
                        {
                            professors.DeleteObject(professor);
                            ins.SaveChanges();
                        }

                        ins.SaveChanges();
                        readers.DeleteObject(element);
                        ins.SaveChanges();
                        bookView.Rows.Remove(bookView.CurrentRow);
                    }
                }
                else
                    bookView.Rows.Remove(bookView.CurrentRow);

            }
            else
            {
                MessageBox.Show("Выберите строку для удаления");
            }
        }
    }
}
